/*
 * Copyright (c) 2022, IMMORTA Inc.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice, this list
 *   of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright notice, this
 *   list of conditions and the following disclaimer in the documentation and/or
 *   other materials provided with the distribution.
 *
 * - Neither the name of IMMORTA Inc. nor the names of its
 *   contributors may be used to endorse or promote products derived from this
 *   software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef TIMER_DRV_H
#define TIMER_DRV_H

/*!
 * @file timer_drv.h
 * @brief This file declares timer driver interfaces
 */

/*!
 * @addtogroup timer_drv
 * @{
 */

/*******Includes***************************************************************/
#include <stdbool.h>
#include "errcodes.h"
#include "clock_drv.h"

/*******Definitions************************************************************/
/*!
 * @brief Timer counter unit
 */
typedef enum {
    TIMER_COUNTER_UNITS_TICKS        = 0U, /*!< Ticks, or clock cycles */
    TIMER_COUNTER_UNITS_MICROSECONDS = 1U  /*!< Micro seconds (us) */
} timer_counter_units_t;

/*!
 * @brief Timer configurations
 */
typedef struct {
    timer_clk_src_t srcClk;             /*!< Source clock */
    bool intEnable;                     /*!< Interrupt enable */
    uint8_t prescaler;                  /*!< Prescaler value */
    uint32_t compareValue;              /*!< Compare value */
    timer_counter_units_t counterUnits; /*!< Compare value units */
} timer_config_t;

/*******APIs*******************************************************************/
#if defined(__cplusplus)
extern "C" {
#endif

/*!
 * @brief Initial a timer instance
 *
 * @param[in] instance: Timer instance number
 * @param[in] config: Pointer to timer configuration structure
 * @param[in] startCounter: true: Start timer counter immediately after init
 *                          false: Don't start timer counter immediately after init
 * @return None
 */
void TIMER_Init(uint32_t instance, const timer_config_t* config, bool startCounter);

/*!
 * @brief Configure a timer instance
 *
 * @param[in] instance: Timer instance number
 * @param[in] config: Pointer to timer configuration structure
 * @return None
 */
void TIMER_SetConfig(uint32_t instance, const timer_config_t* config);

/*!
 * @brief Get current configuration of a timer instance
 *
 * @param[in] instance: Timer instance number
 * @param[out] config: Pointer to timer configuration structure
 * @return None
 */
void TIMER_GetConfig(uint32_t instance, timer_config_t* config);

/*!
 * @brief De-initialize a timer instance
 *
 * @param[in] instance: Timer instance number
 * @return None
 */
void TIMER_Deinit(uint32_t instance);

/*!
 * @brief Set the compare value in counter tick units for a timer instance
 *
 * @param[in] instance: Timer instance number
 * @param[in] compareValueByCount: The compare value in counter ticks
 * @return ERR_SUCCESS: Set compare value success
 *         ERR_ERROR: Cannot configure compare value
 */
errcode_t TIMER_SetCompareValueByCount(uint32_t instance, uint32_t compareValueByCount);

/*!
 * @brief Get current compare value in counter ticks
 *
 * @param[in] instance: Timer instance number
 * @param[out] compareValueByCount: Pointer to the current compare value in counter ticks
 * @return None
 */
void TIMER_GetCompareValueByCount(uint32_t instance, uint32_t* compareValueByCount);

/*!
 * @brief Set the compare value in micro seconds for a timer instance
 *
 * @param[in] instance: Timer instance number
 * @param[in] compareValueByUs: The compare value in micro seconds
 * @return ERR_SUCCESS: Set compare value success
 *         ERR_ERROR: Cannot configure compare value
 */
errcode_t TIMER_SetCompareValueByUs(uint32_t instance, uint32_t compareValueByUs);

/*!
 * @brief Get current compare value in micro seconds
 *
 * @param[in] instance: Timer instance number
 * @param[out] compareValueByUs: Pointer to the current compare value in micro seconds
 * @return None
 */
void TIMER_GetCompareValueByUs(uint32_t instance, uint32_t* compareValueByUs);

/*!
 * @brief Get compare flag
 *
 * @param[in] instance: Timer instance number
 * @return true: Compare flag is valid
 *         false: Compare flag is invalid
 */
bool TIMER_GetCompareFlag(uint32_t instance);

/*!
 * @brief Clear compare flag
 *
 * @param[in] instance: Timer instance number
 * @return None
 */
void TIMER_ClearCompareFlag(uint32_t instance);

/*!
 * @brief Check timer counter status
 *
 * @param[in] instance: Timer instance number
 * @return true: Timer counter is running
 *         false: Timer counter is not running
 */
bool TIMER_IsRunning(uint32_t instance);

/*!
 * @brief Enable or disable timer interrput
 *
 * @param[in] instance: Timer instance number
 * @param[in] enable: true: Enable
 *                    false: Disable
 * @return None
 */
void TIMER_SetIntEnable(uint32_t instance, bool enable);

/*!
 * @brief Get current timer counter value
 *
 * @param[in] instance: Timer instance number
 * @return Current counter value
 */
uint32_t TIMER_GetCounterValueByCount(uint32_t instance);

/*!
 * @brief Start timer counter
 *
 * @param[in] instance: Timer instance number
 * @return None
 */
void TIMER_StartCounter(uint32_t instance);

/*!
 * @brief Stop timer counter
 *
 * @param[in] instance: Timer instance number
 * @return None
 */
void TIMER_StopCounter(uint32_t instance);

#if defined(__cplusplus)
}
#endif

/*! @} */

#endif /* TIMER_DRV_H */

/*******EOF********************************************************************/

